package pri.jz.keeper.util

import android.content.Context
import android.net.ConnectivityManager
import android.net.LinkProperties
import android.net.Network
import android.net.NetworkCapabilities
import android.util.Log
import androidx.compose.runtime.MutableState
import pri.jz.keeper.eventbus.EventBusCode
import pri.jz.keeper.eventbus.EventBusMsg
import pri.jz.keeper.eventbus.EventBusUtil

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS: 网络工具
 */
object NetworkUtil {
    const val TAG = "NetworkUtil"
    var networkState: MutableState<Int>? = null

    /**
     * 注册网络监听
     * @param context
     */
    fun registerNetworkCallback(context: Context) {
        (context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager)
            .registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback(){
                /**
                 * 当框架连接并声明一个新的网络可供使用时调用。
                 *Called when the framework connects and
                 * has declared a new network ready for use.
                 */
                override fun onAvailable(network: Network) {
                    super.onAvailable(network)
                    EventBusUtil.sendMsg(EventBusMsg(EventBusCode.CODE_NETWORK,EventBusCode.CODE_NETWORK_AVAILABLE))
                    networkState?.value = 1
                    Log.d(TAG, "onAvailable>>>>>start")
                }

                /**
                 *当对指定网络的访问被阻止或解除阻止时调用。
                 * Called when access to the specified
                 * network is blocked or unblocked.
                 */
                override fun onBlockedStatusChanged(network: Network, blocked: Boolean) {
                    super.onBlockedStatusChanged(network, blocked)
                    EventBusUtil.sendMsg(EventBusMsg(EventBusCode.CODE_NETWORK,EventBusCode.CODE_NETWORK_BLOCKED,blocked))

                    Log.d(TAG, "onBlockedStatusChanged>>>>>start")
                }

                /**
                 * 当与此请求对应的网络更改功能但仍满足请求的条件时调用。
                 * Called when the network corresponding to this
                 * request changes capabilities but still satisfies the requested criteria.
                 */
                override fun onCapabilitiesChanged(
                    network: Network,
                    networkCapabilities: NetworkCapabilities
                ) {
                    super.onCapabilitiesChanged(network, networkCapabilities)
                    EventBusUtil.sendMsg(EventBusMsg(EventBusCode.CODE_NETWORK,EventBusCode.CODE_NETWORK_CAPABILITIES))
                    Log.d(TAG, "onCapabilitiesChanged>>>>>start")

                }

                /**
                 * 当该请求对应的网络发生变化时调用LinkProperties。
                 * Called when the network corresponding
                 * to this request changes LinkProperties.
                 */
                override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
                    super.onLinkPropertiesChanged(network, linkProperties)
                    EventBusUtil.sendMsg(EventBusMsg(EventBusCode.CODE_NETWORK,EventBusCode.CODE_NETWORK_LINK_PROPERTIES))
                    Log.d(TAG, "onLinkPropertiesChanged>>>>>start")
                }

                /**
                 * 当网络即将丢失时调用，通常是因为没有未完成的请求。
                 * Called when the network is about to be lost,
                 * typically because there are no outstanding requests left for it.
                 */
                override fun onLosing(network: Network, maxMsToLive: Int) {
                    super.onLosing(network, maxMsToLive)
                    EventBusUtil.sendMsg(EventBusMsg(EventBusCode.CODE_NETWORK,EventBusCode.CODE_NETWORK_LOSING))
                    Log.d(TAG, "onLosing>>>>>start")
                }

                /**
                 * 当网络断开连接或不再满足此请求或回调时调用。
                 * Called when a network disconnects or otherwise
                 * no longer satisfies this request or callback.
                 */
                override fun onLost(network: Network) {
                    super.onLost(network)
                    EventBusUtil.sendMsg(EventBusMsg(EventBusCode.CODE_NETWORK,EventBusCode.CODE_NETWORK_LOST))
                    networkState?.value = -1
                    Log.d(TAG, "onReceive>>>>>网络中断")
                }

                /**
                 * 如果在调用中指定的超时时间内没有找到网络，
                 * 或者如果请求的网络请求无法完成（无论是否指定了超时），则调用。
                 * Called if no network is found
                 * within the timeout time specified in ConnectivityManager.
                 * requestNetwork(android.net.NetworkRequest,
                 * android.net.ConnectivityManager.NetworkCallback, int)
                 * call or if the requested network request cannot
                 * be fulfilled (whether or not a timeout was specified).
                 *
                 */
                override fun onUnavailable() {
                    super.onUnavailable()
                    EventBusUtil.sendMsg(EventBusMsg(EventBusCode.CODE_NETWORK,EventBusCode.CODE_NETWORK_UNAVAILABLE))
                    networkState?.value = -1
                    Log.d(TAG, "onUnavailable>>>>>start")
                }
            })
    }

}